<?php
class ModelMyplanUser extends Model{
	
	public function getAllUsers($data){
	
		$sql="select user_id,account,nickname from myplan_user";
		
		if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		return $this->db->query($sql)->rows;
	}
	
	public function getTotalUsers(){
	
		$sql="select COUNT(*) as total from myplan_user";
		
		return $this->db->query($sql)->row["total"];
	}
	
	public function getUserByNickname($data){
		return $this->db->query("select user_id,account,nickname from myplan_user where nickname='".$this->db->escape($data['nickname'])."'")->rows; 
	}
	
	public function getUserById($user_id){
		return $this->db->query("select * from myplan_user where user_id='".(int)$user_id."'")->row; 
	}
	
	//friends 
	public function addToFriend($data){
			$this->db->query("insert into myplan_friendslist set user_id='".$this->db->escape($data['user_id'])."',friend_id='".$this->db->escape($data['friend_id'])."',date_added=NOW()");
	}
	
	public function getAllFriends($user_id){
		return $this->db->query("select friend_id from myplan_friendslist where user_id='".(int)$user_id."'")->rows; 
	}

	public function isFriend($data){
		$num=$this->db->query("select user_id from myplan_friendslist where user_id='".$data['user_id']."' and friend_id='".
				$data['friend_id']."'")->num_rows;	
		if($num!=0){
			return true;
		}
		
		return false;
	}
	
	//information
	//info_type 
	public function addInfo($data){
		$this->db->query("insert into myplan_information set user_id='".$this->db->escape($data['user_id'])."',content='".$this->db->escape($data['content'])."',params='".$this->db->escape($data['params'])."',date_added=NOW()");
	}
	
	public function getAllInfo($data){
		return $this->db->query("select * from myplan_information where user_id='".$data['user_id']."'")->rows;
	}
	
	public function getInfoById($info_id){
		return $this->db->query("select * from myplan_information where info_id='".$info_id."'")->row;
	}
	
	public function deleteInfoById($info_id){
		return $this->db->query("delete from myplan_information where info_id='".$info_id."'");
	}
	
	//add friend request
	public function addFriendRequest($data){
		$this->db->query("insert into myplan_friendadd_request set from_user_id='".$this->db->escape($data['from_user_id'])."',from_account='".$this->db->escape($data['from_account'])."',to_user_id='".$this->db->escape($data['to_user_id'])."',date_added=NOW()");
	}
	
	public function getAllFriendaddRequest($data){
		return $this->db->query("select * from myplan_friendadd_request where to_user_id='".$data['user_id']."'")->rows;
	}
	
	public function deleteRequest($data){
		$this->db->query("delete from myplan_friendadd_request where from_user_id='".$data["friend_id"]."'");
	}
	
	public function hasRequest($data){
		$nums=$this->db->query("select * from myplan_friendadd_request where to_user_id='".$data['to_user_id']."' and from_user_id='".$data['from_user_id']."'")->num_rows;
		
		if($nums==0){
			return false;
		}
		
		return true;
	}
	
	//news count.
	public function getNewsCount($user_id){
		$countinfo=$this->db->query("select COUNT(*) as total from myplan_information where user_id='".$user_id."'")->row["total"];
		
		$countrequest=$this->db->query("select COUNT(*) as total from myplan_friendadd_request where to_user_id='".$user_id."'")->row["total"];
		
		return $countinfo+$countrequest;
	}
	
	public function getFriendsNews($data){
	
		$allfriends=$this->getAllFriends($data["user_id"]);
	
		$sql="select * from myplan_friendsnews f,myplan_user u";
		
		if(count($allfriends)>0){
			$sql.=" where f.user_id in (";
			
			$i=0;
			
			foreach($allfriends as $friend){
				if($i==0){
					$sql.=$friend["friend_id"];
				}else{
					$sql.=",".$friend["friend_id"];
				}
				$i++;
			}
			
			$sql.=") and f.user_id=u.user_id";
			
		}
		
		$sql.=" order by f.date_added desc";
		
		if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		return $this->db->query($sql)->rows;
	}
	
	public function getTotalFriendsNews($data){
		$allfriends=$this->getAllFriends($data["user_id"]);
	
		$sql="select COUNT(*) as total from myplan_friendsnews";
		
		if($allfriends->num_rows>0){
			$sql.=" where user_id in (";
			
			foreach($allfriends as $friend){
				$sql.=$friend["friend_id"];
			}
			
			$sql.=")";
			
		}
		
		return $this->db->query($sql)->row["total"];
	}
}
?>
